package com.zxn.dragdemo;

import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zxn.drag.ItemDragHelper;
import com.zxn.drag.callback.OnItemDragListener;
import com.zxn.drag.widget.DeleteAreaLayout;
import com.zxn.dragdemo.adapter.ImageItemAdapter;
import com.zxn.dragdemo.bean.PictureInfo;

import java.util.ArrayList;

/**
 * ItemDragHelper
 * AnimUtils
 * AnimationUtils
 * Updated by zxn on 2020/3/18.
 */
public class DragActivity extends AppCompatActivity {

    private static final String TAG = DragActivity.class.getSimpleName();
    //@BindView(R.id.ll_delete_icon)
    LinearLayout llDeleteIcon;
    //@BindView(R.id.recy)
    RecyclerView mRecyclerView;
    //@BindView(R.id.delete_area)
    DeleteAreaLayout mRrDeleteArea;

    private ImageItemAdapter mAdapter;
    private ItemDragHelper mItemDragHelper;

    private GridLayoutManager manager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag);
        //ButterKnife.bind(this);
        initView();
        onInitView();

        onInitData();

        onInitItemDragHelper();
        initView();
    }

    private void onInitItemDragHelper() {
        mItemDragHelper = new ItemDragHelper(mRrDeleteArea);
        mItemDragHelper.setOnDragListener(new OnItemDragListener() {
            @Override
            public void onDragStart() {
                Log.i(TAG, "onDragStart: ");
                mRrDeleteArea.showDragStartView();
            }

            @Override
            public void onDragFinish(boolean isInside, int position) {
                Log.i(TAG, "onDragFinish: isInside:" + isInside);
                if (isInside) {
                    mRrDeleteArea.showDragFinishView();
                    mAdapter.getData().remove(position);
                    mAdapter.notifyDataSetChanged();
                } else {
                    mRrDeleteArea.hideView();
                }
            }

            @Override
            public void onDragAreaChange(boolean isInside, boolean isIdle) {
                Log.i(TAG, "onDragAreaChange: --isInside:" + isInside + "--isIdle:" + isIdle);
                if (isIdle) {
                    return;
                }
                mRrDeleteArea.showDragAreaChangeView(isInside);
            }
        });
        mItemDragHelper.attachToRecyclerView(mRecyclerView);
    }

    private void onInitData() {
        ArrayList<PictureInfo> list = new ArrayList<>();
        PictureInfo pic;
        for (int i = 0; i < 9; i++) {
            pic = new PictureInfo();
            pic.id = i;
            pic.path = "https://public-1254365843.cos.ap-beijing.myqcloud.com/illustration/share/00021802_List_Picture.jpg";
            list.add(pic);
        }
        mAdapter.setNewData(list);
    }

    private void onInitView() {
        manager = new GridLayoutManager(this, 3);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new ImageItemAdapter();
        mRecyclerView.setAdapter(mAdapter);

    }

    private void initView() {
        mRrDeleteArea = (DeleteAreaLayout) findViewById(R.id.delete_area);
        mRecyclerView = (RecyclerView) findViewById(R.id.recy);
    }
}
